//ANIOUT v.0.5 
//from a source fla containing the animatic, generates an fla for each sequence, based on the labels
//to do: generate PNGS for each fla, generalte FCP XML
//IMPORTANT: -there should only be one open document.
//			 -it is recommended to make the original fla´s project settings the default project settings.
//
// Advisabe to have the version cue enabled feature on, adobelike dopen¡and save dialog.
doc = fl.getDocumentDOM();
tl = doc.getTimeline();
lay = tl. layers;
minombre = doc.name;
var selfr = tl.getSelectedFrames();
var directorio =  fl.browseForFolderURL("Select a folder.");
var labelsArray=new Array();//es nuevo, si no fuinciona cepillar alegremente

var crea = function (){
	fl.createDocument();
	fl.getDocumentDOM().getTimeline().pasteFrames(0);
	//fl.closeDocument(fl.getDocumentDOM(),true);
	doc.editScene(0);
}
function selNone(){
		
	kklayer= doc.getTimeline().addNewLayer("kk");
	doc.getTimeline().deleteLayer (kklayer);
}//manda guebos tener que implementar esto.

function salvatodas(){
	lento=fl.documents.length; //si no, lo recalcula cada vez
	for (inx=1; inx< lento; inx++){
		//copia el nombre 
		namae = labelsArray[inx-1];
		fl.clipCopyString(namae); 
		//save and close el document
		fl.saveDocumentAs(fl.documents[1]);
		fl.closeDocument(fl.documents[1], false);
	}
	
}

function renderAll(){
	lento=fl.documents.length; //si no, lo recalcula cada vez
	for (inx=1; inx< lento; inx++){
		//copia el nombre 
		namae = labelsArray[inx-1];
		fl.clipCopyString(namae); 
		uripng = directorio+"/"+namae ;//solo vale en mac
		FLfile.createFolder (uripng);
		uripng += "/";
		fl.documents[inx].exportPNG(uripng, true , false);
		
	}
}
selNone();//empezamos limpio

//Check the labels layer
var found = false;
var labelsArray = new Array();
var TCArray = new Array();
var durArray = new Array();
var index = 0;

for (var t=0; t <(lay.length);t++){
	fl.trace (t);
	
	
	if (lay[t].name == "labels"){
		//alert ("layer labels encontrado");
		found = true;
		index = t;
	}
}
if (found == false){
	alert ("Layer named labels not found.");

}
else{
	for (var t=0; t< lay[index].frameCount; t<1 ){
		if ((lay[index].frames [t].LabelType) != "none"){
			labelsArray.push (lay[index].frames [t].name);
			TCArray.push (t);
			//la loingitud del TCArray
			long = TCArray.length;
			
			fl.trace ("TCArray"+TCArray);
			fl.trace ("TCArray[t]");
			fl.trace (t);
			fl.trace ("o lo que es o mismo");
			fl.trace (TCArray[long-1]);
			fl.trace ("mas conocido como");
			fl.trace (labelsArray[long-1]);
					
			durArray.push (lay[index].frames [t].duration);
			//doc.getTimeline().setSelectedFrames ([0,t,(t+lay[index].frames [t].duration)]);
			//esta es la buena sif falla el loop
			for (cadalayer=0; cadalayer< ((doc.getTimeline().layers).length); cadalayer++){
		
				doc.getTimeline().setSelectedFrames ([cadalayer,t,(t+lay[index].frames [t].duration)], false);
			
				fl.trace ("layer"+cadalayer+"selected frames de "+ (t)+"....>"+(t+lay[index].frames [t].duration));
				
				
			}
			
			
			doc.getTimeline().copyFrames ();
			selNone();	//asi desselecciona	
			t = t+lay[index].frames [t].duration;
			crea ();
	
		}
	}
}

fl.trace ("labelsArray");
fl.trace (labelsArray);

fl.trace ("frames");
fl.trace (TCArray);

fl.trace ("durArray");
fl.trace (durArray);
alert ("Ready to render!");
renderAll();
salvatodas();

//fl.clipCopyString(string) 
//para copiar el nombre del plano si hay que hacer un save as
//var folderContents = FLfile.listFolder(folderURI); 

//para el tema del quicktime
/*alert(fl.getDocumentDOM().importPublishProfile('file:///C|/Documents and 
Settings/janeUser/Desktop/profile.xml')); 
*/
